package main

import (
	"crypto/hmac"
	"crypto/sha1"
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
)




type params struct {
	uid int
	source int
	receiver_id int
	template_id string
	data string
}

type data struct{

}
func main(){
	resp := httpDo()
	fmt.Println(string(resp))
}


func httpDo() []byte {
	client := &http.Client{}
	req,err := http.NewRequest("POST", "http://i.api.weibo.com/2/eps/i_template/send.json", strings.NewReader(""))
	if err != nil{
		fmt.Println(err)
	}
	auth := &Auth{}
	header := auth.buildHttpHeader("1748248662", "OXOWROTSUUNNXNXONYNZPSS=OUPYOTZQPXOTQOQQRTNOXNXOX8M9lkbl$uB", "6020d842bcbce185c38f")
	fmt.Println(header)
	req.Header.Set("Authorization", header)
	resp , err := client.Do(req)
	defer resp.Body.Close()
	if err != nil{
		fmt.Println(err)
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil{
		fmt.Println(err)
	}
	return body
}

type Auth struct {
	token string
	param string
	sign  string
}

type tAuth2 interface{
	genUidString(string) string
	genHmacSHA1([]byte, []byte) []byte
	genSign(string, string) string
	genTAuth2() string
	buildHttpHeader() string
}

func (a Auth) genHmacSHA1(ciphertext, key []byte) []byte{
	mac := hmac.New(sha1.New, key)
	mac.Write(ciphertext)
	return mac.Sum(nil)
}
func (a Auth) genUidString(uid string) string{
	return fmt.Sprintf("uid=%s", uid)
}
func (a Auth)genSign(str, secret string) string {
	s := a.genHmacSHA1([]byte(str), []byte(secret))
	sEnc := base64.StdEncoding.EncodeToString(s)
	return sEnc
}

func (a Auth) buildHttpHeader(uid, tAuthToken, tAuthTokenSecret string) string{
	param := a.genUidString(uid)
	sign := a.genSign(uid, tAuthTokenSecret)
	token := url.QueryEscape(tAuthToken)
	param = url.QueryEscape(param)
	sign = url.QueryEscape(sign)
	return fmt.Sprintf("TAuth2 token=\"%s\",param=\"%s\",sign=\"%s\"", token, param, sign)
}